package com.ark.controller.admin;

import com.ark.constant.CacheConstant;
import com.ark.dto.DishDTO;
import com.ark.dto.DishPageQueryDTO;
import com.ark.entity.Dish;
import com.ark.result.PageResult;
import com.ark.result.Result;
import com.ark.service.DishService;
import com.ark.vo.DishVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Tag(name = "菜品相关接口")
@Slf4j
@RequiredArgsConstructor
public class DishController {

    private final DishService dishService;

    private final RedisTemplate<String,Object> redisTemplate;

    @PostMapping
    @Operation(summary = "新增菜品")
    public Result<?> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品: {}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key = CacheConstant.DISH_HEAD +dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    @GetMapping("/page")
    @Operation(summary = "菜品分页查询")
    public Result<PageResult<DishVO>> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询: {}", dishPageQueryDTO);
        PageResult<DishVO> pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    @Operation(summary = "菜品批量删除")
    public Result<?> delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除: {}",ids);
        dishService.deleteBatch(ids);
        //简单处理，删除所有dish_开头的key
        cleanCache(CacheConstant.DISH_HEAD+"*");
        return Result.success();
    }

    @Operation(summary = "根据id查询菜品")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品: {}",id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }


    @PutMapping
    @Operation(summary = "修改菜品")
    public Result<?> update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品: {}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //简单处理
        //简单处理，删除所有dish_开头的key
        cleanCache(CacheConstant.DISH_HEAD+"*");
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @Operation(summary = "启用禁用菜品")
    public Result<?> stopOrStart(@PathVariable Integer status,Long id){
        dishService.stopOrStart(status,id);
        //简单处理，删除所有dish_开头的key
        cleanCache(CacheConstant.DISH_HEAD+"*");
        return Result.success();
    }

    @GetMapping("/list")
    @Operation(summary = "根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    private void cleanCache(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
